package org.gdstash.character;

import org.gdstash.file.GDReader;
import org.gdstash.file.GDWriter;

import java.io.IOException;

public class GDCharUISlot {
    public static final int TYPE_NONE = -1;
    public static final int TYPE_SKILL = 0;
    public static final int TYPE_ITEM = 4;
    private int type;
    private String skill;
    private byte isItemSkill;
    private String item;
    private int equipLocation;
    private String bitmapUp;
    private String bitmapDown;
    private String label;

    public void read() throws IOException {
        this.type = GDReader.readEncInt(true);
        if (this.type == 0) {
            this.skill = GDReader.readEncString();
            this.isItemSkill = GDReader.readEncByte();
            this.item = GDReader.readEncString();
            this.equipLocation = GDReader.readEncInt(true);
        } else if (this.type == 4) {
            this.item = GDReader.readEncString();
            this.bitmapUp = GDReader.readEncString();
            this.bitmapDown = GDReader.readEncString();
            this.label = GDReader.readEncWideString();
        }
    }

    public void write() throws IOException {
        GDWriter.writeInt(this.type);
        if (this.type == 0) {
            GDWriter.writeString(this.skill);
            GDWriter.writeByte(this.isItemSkill);
            GDWriter.writeString(this.item);
            GDWriter.writeInt(this.equipLocation);
        } else if (this.type == 4) {
            GDWriter.writeString(this.item);
            GDWriter.writeString(this.bitmapUp);
            GDWriter.writeString(this.bitmapDown);
            GDWriter.writeWideString(this.label);
        }
    }

    public int getByteSize() {
        int size = 0;
        size += 4;
        if (this.type == 0) {
            size += 4;
            if (this.skill != null) {
                size += this.skill.length();
            }
            ++size;
            size += 4;
            if (this.item != null) {
                size += this.item.length();
            }
            size += 4;
        } else if (this.type == 4) {
            size += 4;
            if (this.item != null) {
                size += this.item.length();
            }
            size += 4;
            if (this.bitmapUp != null) {
                size += this.bitmapUp.length();
            }
            size += 4;
            if (this.bitmapDown != null) {
                size += this.bitmapDown.length();
            }
            size += 4;
            if (this.label != null) {
                size += this.label.length() * 2;
            }
        }
        return size;
    }
}
